package com.eco.controller;

import com.alibaba.fastjson.JSONObject;
import com.eco.annotation.AuthToken;
import com.eco.constants.Constants;
import com.eco.netty.NettyClientConfig;
import com.eco.schedule.LightScheduleTask;
import com.eco.utils.KndUtil;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@Data
public class LightController {


    private final NettyClientConfig nettyClientConfig;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    
    private final LightScheduleTask lightScheduleTask;;

    public LightController(LightScheduleTask lightScheduleTask, NettyClientConfig nettyClientConfig) {
        this.lightScheduleTask = lightScheduleTask;
        this.nettyClientConfig = nettyClientConfig;
    }


    @RequestMapping({"/setWaterLevel"})
    @AuthToken("admin")
    public JSONObject setWaterLevel(String waterLevel) {
        JSONObject result = new JSONObject();
        waterLevel = StringUtils.trim(waterLevel);
        if (StringUtils.isEmpty(waterLevel) || !NumberUtils.isCreatable(waterLevel)) {
            log.error("水位阈值参数为空或不是数字");
            result.put("msg", "水位阈值参数为空或不是数字");
            result.put("status", Boolean.valueOf(false));
            return result;
        }
        if (StringUtils.equals("0", waterLevel)) {
            this.stringRedisTemplate.opsForValue().setBit("autoStatus", 1L, false);
            log.info("关闭自动控制诱鱼灯");
        } else {
            this.stringRedisTemplate.opsForValue().setBit("autoStatus", 1L, true);
            log.info("成功设置水位阈值" + waterLevel);
        }
        this.stringRedisTemplate.opsForValue().set("waterLevel", waterLevel);
        result.put("msg", "成功设置水位阈值" + waterLevel);
        result.put("status", Boolean.valueOf(true));
        return result;
    }

    @RequestMapping({"/getWaterLevel"})
    public JSONObject getWaterLevel() {
        JSONObject result = new JSONObject();
        result.put("status", Boolean.valueOf(true));
        result.put("waterLevel", this.stringRedisTemplate.opsForValue().get("waterLevel"));
        return result;
    }

    @RequestMapping({"/getYydStatus"})
    public JSONObject getYydStatus() {
        JSONObject result = new JSONObject();
        Boolean redLamp = this.stringRedisTemplate.opsForValue().getBit("yyd", 1L);
        Boolean blueLamp = this.stringRedisTemplate.opsForValue().getBit("yyd", 2L);
        Boolean greenLamp = this.stringRedisTemplate.opsForValue().getBit("yyd", 3L);
        JSONObject yyd = new JSONObject();
        if (redLamp.booleanValue() && blueLamp.booleanValue() && greenLamp.booleanValue()) {
            yyd.put("num", Integer.valueOf(6));
            yyd.put("state", Boolean.valueOf(true));
        } else if (redLamp.booleanValue() && blueLamp.booleanValue()) {
            yyd.put("num", Integer.valueOf(4));
            yyd.put("state", Boolean.valueOf(true));
        } else if (redLamp.booleanValue() && greenLamp.booleanValue()) {
            yyd.put("num", Integer.valueOf(3));
            yyd.put("state", Boolean.valueOf(true));
        } else if (blueLamp.booleanValue() && greenLamp.booleanValue()) {
            yyd.put("num", Integer.valueOf(5));
            yyd.put("state", Boolean.valueOf(true));
        } else if (greenLamp.booleanValue()) {
            yyd.put("num", Integer.valueOf(2));
            yyd.put("state", Boolean.valueOf(true));
        } else if (blueLamp.booleanValue()) {
            yyd.put("num", Integer.valueOf(1));
            yyd.put("state", Boolean.valueOf(true));
        } else if (redLamp.booleanValue()) {
            yyd.put("num", Integer.valueOf(0));
            yyd.put("state", Boolean.valueOf(true));
        } else {
            yyd.put("num", Integer.valueOf(7));
            yyd.put("state", Boolean.valueOf(false));
        }
        result.put("yyd", yyd);
        return result;
    }

    @RequestMapping({"/getLampStatus"})
    public JSONObject getLampStatus() {
        JSONObject result = new JSONObject();
        String lightStatus = this.stringRedisTemplate.opsForValue().get(Constants.SENSOR_REDIS_KEY);
        result.put("openValue1", Constants.SENSOR_ON.equals(lightStatus));
        return result;
    }




    @ApiOperation("site是sensor,order 打开是turnon关闭是turnoff")
    @RequestMapping({"/order2Knd"})
    @AuthToken("admin")
    public JSONObject order2Knd(String site, String order) {
        JSONObject result = new JSONObject();
        site = StringUtils.trim(site);
        order = StringUtils.trim(order);
        if (StringUtils.isEmpty(site) || StringUtils.isEmpty(order)) {
            log.error("点位或指令参数为空");
            result.put("msg", "点位或指令参数为空");
            result.put("status", Boolean.valueOf(false));
            return result;
        }
        KndUtil.sendCommand(nettyClientConfig.getClient1().getClientId(), order);
        KndUtil.sendCommand(nettyClientConfig.getClient2().getClientId(), order);
        stringRedisTemplate.opsForValue().set(Constants.SENSOR_REDIS_KEY,"turnoff".equals(order)?Constants.SENSOR_OFF:Constants.SENSOR_ON);
        result.put("status", true);
        return result;
    }


    @GetMapping("getLambScheduleSetting")
    public JSONObject getLambScheduleSetting() {
        JSONObject result = new JSONObject();
        result.put("status", true);
        String start = stringRedisTemplate.opsForValue().get(Constants.LIGHT_SCHEDULE_START_KEY);
        String stop = stringRedisTemplate.opsForValue().get(Constants.LIGHT_SCHEDULE_STOP_KEY);
        result.put("start",start);
        result.put("stop",stop);
        return result;
    }

    @ApiOperation("修改灯带定时开关时间")
    @GetMapping("updateLambScheduleSetting")
    public JSONObject updateLambScheduleSetting(String start,String stop) {
        stringRedisTemplate.opsForValue().set(Constants.LIGHT_SCHEDULE_START_KEY,start);
        stringRedisTemplate.opsForValue().set(Constants.LIGHT_SCHEDULE_STOP_KEY,stop);
        String[] startSplit = start.split(":");
        String[] stopSplit = stop.split(":");
        JSONObject result = new JSONObject();
        //0 35 12 * * ?
        String startCron = startSplit[2]+" "+startSplit[1]+" "+startSplit[0]+" * * ?";
        String stopCron = stopSplit[2]+" "+stopSplit[1]+" "+stopSplit[0]+" * * ?";;
        lightScheduleTask.updateCronExpression(startCron,stopCron);
        result.put("status", true);
        return result;
    }
}
